package com.cljz.peipei.present.activity;

import com.cljz.peipei.base.BaseP;
import com.cljz.peipei.model.net.api.Api;
import com.cljz.peipei.model.net.bean.ResponseBean;
import com.cljz.peipei.model.net.bean.ThirdLoginBean;
import com.cljz.peipei.ui.activity.LoginActivity;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;

/**
 * Created by apple on 2017/4/17.
 */

public class LoginP extends BaseP<LoginActivity> {
    public void sendSmsCode(String phone) {
        Api.getResponseInfoAPI().sendSmsData(phone, "sms_login")
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean sendSmsCodeBean) {
                        if ("0".equals(sendSmsCodeBean.getCode())) {
                            getV().sendSmsSucceed(sendSmsCodeBean);
                        } else {
                            getV().sendSmsFail(sendSmsCodeBean.getMsg());
                        }
                    }
                });

    }

    public void loginByThird(SHARE_MEDIA platform, Map<String, String> data) {
        Api.getResponseInfoAPI().thirdLoginData(data.get("openid"), data.get("access_token"))
                .compose(XApi.<ThirdLoginBean>getApiTransformer())
                .compose(XApi.<ThirdLoginBean>getScheduler())
                .compose(getV().<ThirdLoginBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ThirdLoginBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(ThirdLoginBean thirdLoginBean) {
                        // TODO: 2017/4/18  判断是否已绑定手机号，未绑定则跳转绑定页面
                    }
                });
    }
}
